*** Settings ***
Resource            ../../test_function/redfish/redfish.robot
Library             Collections
Library             RequestsLibrary
Library             BuiltIn
Suite Setup         Create REDFISH_POST Session
Suite Teardown      Delete All Sessions

*** Variables ***
${INPUT_URI}        /SessionService/Sessions

*** Test Cases ***
BMC Login Via Redfish
    [Documentation]    验证通过Redfish协议登录BMC的功能
    [Tags]    BMC_Login_Via_Redfish
    ${headers}=    Create Dictionary    Content-Type=application/json
    ${body}=    Create Dictionary
    ...    UserName=${OPENUBMC_USERNAME}
    ...    Password=${OPENUBMC_PASSWORD}

    ${rsp}=    Run Keyword And Ignore Error    REDFISH_POST Request
    ...    url=${INPUT_URI}
    ...    body=${body}
    ...    expected_status=201
    ...    headers=${headers}

    Run Keyword If     '${rsp[0]}' == 'FAIL'    Fail    msg=refish登录bmc失败: ${rsp[1]}

    # 验证响应状态码
    Should Be Equal As Strings    ${rsp[1].status_code}    201    msg=预期状态码201,实际收到${rsp[1].status_code}

    # 打印响应内容
    Log    ${rsp[1].json()}    level=INFO

    # 检查响应中是否包含X-Auth-Token
    Should Contain X-Auth-Token    ${rsp[1].headers}
    Should Be Valid Session URI    ${rsp[1].json()}

*** Keywords ***
Should Contain X-Auth-Token
    [Arguments]    ${headers}
    Dictionary Should Contain Key    ${headers}    X-Auth-Token
    Should Not Be Empty    ${headers['X-Auth-Token']}

Should Be Valid Session URI
    [Arguments]    ${json_data}
    Dictionary Should Contain Key    ${json_data}    @odata.id
    Should Match Regexp    ${json_data['@odata.id']}  /redfish/v1/SessionService/Sessions/[a-fA-F0-9]+

